package com.mybatis.demo1;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MainApp {
	private static SqlSessionFactory sqlSessionFactory;
	private static Reader reader;
	static{
		try{
			reader = Resources.getResourceAsReader("mybatis1.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 返回单实例(配置)
	 */
	public void testDemo1(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user = (User) session.selectOne("com.mybatis.demo1.UserOperation.selectUserByID", 1);
			System.out.println(user.getName());
			System.out.println(user.getPwd());
		} finally {
			session.close();
		}
	}
	/**
	 * 以接口方式编程(注意：User.xml的namespace需要改成接口的路径)
	 */
	public void testDemo1_(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserOperation userOperation = session.getMapper(UserOperation.class);
			User user = userOperation.selectUserByID(1);
			System.out.println(user.getName());
			System.out.println(user.getPwd());
		} finally {
			session.close();
		}
	}
	/**
	 * 返回多实例(配置)
	 */
	public void testDemo2(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			List<Object> list =(List<Object>) session.selectList("com.mybatis.demo1.UserOperation.selectUsers", "%");
			System.out.println("list:"+list);
		} finally {
			session.close();
		}
	}
	/**
	 * 返回多实例(接口)
	 */
	public void testDemo2_(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserOperation userOperation = session.getMapper(UserOperation.class);
			List<User> list = userOperation.selectUsers("%");
			System.out.println("接口list:"+list);
		} finally {
			session.close();
		}
	}
	/**
	 * 添加一条记录(配置)
	 */
	public void testDemo3(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user=new User();
			user.setName("人民广场d$");
			user.setPwd("飞鸟");
			int flag = session.insert("com.mybatis.demo1.UserOperation.addUser",user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	/**
	 * 添加一条记录(接口)
	 */
	public void testDemo3_(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserOperation userOperation = session.getMapper(UserOperation.class);
			User user=new User();
			user.setName("人民广场接口");
			user.setPwd("飞鸟");
			int flag = userOperation.addUser(user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	/**
	 * 更新一条数据(配置)
	 */
	public void testDemo4(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user=new User();
			user.setName("人民广场");
			user.setPwd("飞鸟2");
			user.setId(12);
			int flag = session.update("com.mybatis.demo1.UserOperation.updateUser",user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	/**
	 * 更新一条记录(接口)
	 */
	public void testDemo4_(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserOperation userOperation = session.getMapper(UserOperation.class);
			User user=new User();
			user.setName("人民广场接口");
			user.setPwd("飞鸟3");
			user.setId(13);
			int flag = userOperation.updateUser(user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	/**
	 * 删除一条数据(配置)
	 */
	public void testDemo5(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user=new User();
			user.setId(2);
			int flag = session.delete("com.mybatis.demo1.UserOperation.deleteUser",user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	/**
	 * 删除一条记录(接口)
	 */
	public void testDemo5_(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			UserOperation userOperation = session.getMapper(UserOperation.class);
			User user=new User();
			user.setId(3);
			int flag = userOperation.deleteUser(user);
			System.out.println("flag:"+flag);
			session.commit();
		} finally {
			session.close();
		}
	}
	public static void main(String[] args) {
//		System.out.println("单实例(配置)============");
		new MainApp().testDemo1();
//		System.out.println("单实例(接口)============");
//		new MainApp().testDemo1_();
//		System.out.println("多实例(配置)============");
//		new MainApp().testDemo2();
//		System.out.println("多实例(接口)============");
//		new MainApp().testDemo2_();
//		System.out.println("添加(配置)============");
		new MainApp().testDemo3();
//		System.out.println("添加(接口)============");
//		new MainApp().testDemo3_();
//		System.out.println("更新(配置)============");
//		new MainApp().testDemo4();
//		System.out.println("更新(接口)============");
//		new MainApp().testDemo4_();
//		System.out.println("删除(配置)============");
//		new MainApp().testDemo5();
//		System.out.println("删除(接口)============");
//		new MainApp().testDemo5_();
	}

}
